# This file is part of the Lumenn project 
# at http://code.google.com/p/lumenn/
#
# Released under the GPLv3 at http://www.gnu.org/copyleft/gpl.html
# Designed for pygtk and the Maemo platform.
#
# Copyright 2008 by Jackson Yee (jackson@gotpossum.com)

import pygtk
pygtk.require('2.0')

import gtk
import gtk.gdk as gdk
import gobject
import pango
import time
import utils
import os
import os.path
import math
import random
import cairo
import pangocairo

from lumenn.geometry import Rect

# *********************************************************************
class Color(object):	
	
	# -------------------------------------------------------------------
	def __init__(self, r, g, b):
		self.r	=	float(r)
		self.g	=	float(g)
		self.b	=	float(b)
		
	# -------------------------------------------------------------------
	def Tuple(self):
		return (self.r, self.g, self.b)
		
	# -------------------------------------------------------------------
	def Draw(self, ctx, rt):
		ctx.set_source_rgb(self.r, self.g, self.b)
		ctx.rectangle(*rt.Tuple())
		ctx.fill()		
		
	# -------------------------------------------------------------------
	def __str__(self):
		return '#%02X%02X%02X' % (self.r * 0xff,
			self.g * 0xff,
			self.b * 0xff,
		)

# =====================================================================
# Convenient colors
white	=	Color(1, 1, 1)
red		=	Color(1, 0, 0)
green	=	Color(0, 0.5, 0)
blue	=	Color(0, 0, 1)
black	=	Color(0, 0, 0)
gray	=	Color(0.5, 0.5, 0.5)

lightgray	=	Color(0.8, 0.8, 0.8)
darkblue	=	Color(0, 0, 0.5)
lime			=	Color(0, 1, 0)
orange		=	Color(1, 0.8, 0)
purple		=	Color(0.5, 0, 0.5)
yellow		=	Color(1, 1, 0)

# *********************************************************************
class Gradient(object):	
	
	# -------------------------------------------------------------------
	def __init__(self, start, middle, end, middlepos):
		self.start	=	start
		self.middle			=	middle
		self.end				=	end
		self.middlepos	=	middlepos
		
	# -------------------------------------------------------------------
	def Draw(self, ctx, rt):
		lg = cairo.LinearGradient(rt.x, rt.y, rt.width, rt.height)
		lg.add_color_stop_rgb(0, *self.start.Tuple())
		lg.add_color_stop_rgb(self.middlepos, *self.middle.Tuple())
		lg.add_color_stop_rgb(1, *self.end.Tuple())
		ctx.set_source(lg)

# *********************************************************************
class HGradient(Gradient):	
	
	# -------------------------------------------------------------------
	def __init__(self, start, middle, end, middlepos):
		Gradient.__init__(self, start, middle, end, middlepos)
		
	# -------------------------------------------------------------------
	def Draw(self, ctx, rt):
		rt.width	= rt.x + rt.width
		rt.height	=	rt.y
		Gradient.Draw(self, ctx, rt)
	
# *********************************************************************
class VGradient(Gradient):	
	
	# -------------------------------------------------------------------
	def __init__(self, start, middle, end, middlepos):
		Gradient.__init__(self, start, middle, end, middlepos)
		
	# -------------------------------------------------------------------
	def Draw(self, ctx, rt):
		rt.width	= rt.x
		rt.height	=	rt.y + rt.height
		Gradient.Draw(self, ctx, rt)

# *********************************************************************
class CairoFX(object):
	
	# -------------------------------------------------------------------
	def __init__(self, ctx):
		self.ctx			=	ctx
		self.font			=	pango.FontDescription('sans 14')
		
	# -------------------------------------------------------------------
	@classmethod
	def randombound(self, lower, upper):
		return random.random() * (upper - lower) + lower
	
	# -------------------------------------------------------------------
	@classmethod
	def bcolor(self):
		return (	self.randombound(0.2, 1),
							self.randombound(0.2, 1),
							self.randombound(0.2, 1),
					)
					
	# -------------------------------------------------------------------
	@classmethod
	def bcolor3(self):
		r = self.bcolor()
		return (	r,
							self.bcolor(),
							r,
					)
					
	# -------------------------------------------------------------------
	@classmethod
	def bcolor2(self):
		b = self.bcolor()
		e = self.bcolor()
		return (	b,
							( (b[0] + e[0]) / 2, (b[1] + e[1]) / 2, (b[2] + e[2]) / 2),
							e,
					)
					
	# -------------------------------------------------------------------
	def SetupCanvas(self, bgcolor, rt):
		ctx = self.ctx
		self.Set(bgcolor)
		ctx.rectangle(*rt.Tuple())
		ctx.clip_preserve()
		ctx.fill()		
		self.canvas	=	rt
		
	# -------------------------------------------------------------------
	def RoundedRect(self, x, y, w, h, r = 0):
		if r == 0:
			r = w / 4
		
		self.move_to(x + r, y)                      
		self.line_to(x + w - r, y)                    
		self.curve_to(x + w, y, x + w, y, x + w, y + r)       
		self.line_to(x + w, y + h - r)                  
		self.curve_to(x + w, y + h, x + w, y + h, x + w - r, y + h) 
		self.line_to(x + r, y + h)                    
		self.curve_to(x, y + h, x, y + h, x, y + h - r)       
		self.line_to(x, y + r)                      
		self.curve_to(x, y, x, y, x + r, y)   
		self.fill()
		
	# -------------------------------------------------------------------
	def DrawText(self, x, y, text):
		layout = self.create_layout()
		layout.set_text(text)
		layout.set_font_description(self.font)
		lw, lh = layout.get_pixel_size()
		self.move_to(x, y)
		self.show_layout(layout)			
		return Rect(x, y, lw, lh)
		
	# -------------------------------------------------------------------
	def Draw(self, o, rt):
		o.Draw(self, rt)
		
	# -------------------------------------------------------------------
	def Set(self, o):
		if isinstance(o, Color):
			self.set_source_rgb( *o.Tuple() )
		elif isinstance(o, pango.FontDescription):
			self.font	=	o
		
	# -------------------------------------------------------------------
	def __getattr__(self, a):
		return getattr(self.ctx, a)
